package com.team8.user.pojo.controller;

import com.team8.common.results.PageResult;
import com.team8.common.results.Result;
import com.team8.user.pojo.User;
import com.team8.user.pojo.service.BookingService;
import com.team8.user.pojo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private BookingService bookingService;


    @Autowired
    private StringRedisTemplate redisTemplate;
    @PostMapping("/register")
    public ResponseEntity<Result> register(@RequestParam("email") String email, @RequestParam("pwd") String pwd, @RequestParam("verifyCode") String verifyCode){

        User user = new User();
        user.setEmail(email);
        user.setUserPwd(pwd);
        user.setRole("1");
        Result insert = userService.insert(user, verifyCode);
        return ResponseEntity.ok(insert);

    }


    @GetMapping("/registerUser")
    public ResponseEntity<Result> registerUser(@RequestParam("email") String email, @RequestParam("pwd") String pwd){


        User user = new User();
        user.setEmail(email);
        user.setUserPwd(pwd);
        user.setRole("0");
        Result insert = userService.insert(user);
        return ResponseEntity.ok(insert);

    }

    @GetMapping("/query")
    public ResponseEntity<User> queryUser(@RequestParam("email") String email, @RequestParam("pwd") String pwd){
        User user = userService.queryUser(email, pwd);
        if(user == null)
            return ResponseEntity.ok(null);
        return ResponseEntity.ok(user);
    }

    @GetMapping("/sendEmail")
    public ResponseEntity<Boolean> sendEmail(@RequestParam("email") String email){
        Boolean aBoolean = userService.sendVerifyCode(email);
        if(aBoolean)
            return ResponseEntity.ok(true);
        return ResponseEntity.ok(false);
    }

    @GetMapping("/booking")
    public ResponseEntity<Result> booking(
            @RequestParam("ffid") String ffid,
            @RequestParam("person_email")String personEmail,
            @RequestParam("start_seg") Integer startSeg,
            @RequestParam("end_seg") Integer endSeg){

        if(endSeg <= startSeg)
            return ResponseEntity.ok(new Result(401, "bad params"));

        Result result = bookingService.booking(ffid, personEmail, startSeg, endSeg);

        return ResponseEntity.ok(result);
    }


    @GetMapping("/delete")
    public ResponseEntity<Result> delete(
            @RequestParam("email") String email){

        Result result = userService.delete(email);

        return ResponseEntity.ok(result);
    }

    @GetMapping("/updatepwd")
    public ResponseEntity<Result> updatepwd(
            @RequestParam("email") String email, @RequestParam("pwd") String pwd, @RequestParam("verifyCode") String verifyCode){

        Result result = userService.findpwd(email, pwd, verifyCode);

        return ResponseEntity.ok(result);
    }


    @GetMapping("/users")
    public ResponseEntity<PageResult<User>> users(@RequestParam("page") Integer page, @RequestParam("size") Integer size){

        PageResult<User> allUsers = userService.getAllUsers(page, size);

        return ResponseEntity.ok(allUsers);
    }

    @GetMapping("/finduser")
    public ResponseEntity<PageResult<User>> finduser(@RequestParam("email") String email, @RequestParam("name") String name,@RequestParam("page") Integer page, @RequestParam("size") Integer size){

        PageResult<User> allUsers = userService.findUsers(email, name, page, size);

        return ResponseEntity.ok(allUsers);
    }

    @GetMapping("/edit")
    public ResponseEntity<Result> edit(@RequestParam("email") String email, @RequestParam("name") String name,@RequestParam("pwd") String pwd){

        Result result = userService.editUser(email, name, pwd);

        return ResponseEntity.ok(result);
    }


    @GetMapping("/adduser")
    public ResponseEntity<Result> adduser(@RequestParam("email") String email, @RequestParam("name") String name,@RequestParam("pwd") String pwd){


        User user = new User();
        user.setEmail(email);
        user.setUserName(name);
        user.setUserPwd(pwd);
        user.setRole("1");
        Result result = userService.insertDiqin(user);

        return ResponseEntity.ok(result);
    }




}
